package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.AdminDao;
import com.ruoyi.reservationserver.entity.Admin;
import com.ruoyi.reservationserver.service.AdminService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员表
(Admin)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:47
 */
@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao,Admin> implements AdminService {
    @Resource
    private AdminDao adminDao;
    @Resource
    private AdminService adminService;

    /**
     * 通过ID查询单条数据
     *
     *
     * @return 实例对象
     */
    @Override
    public PageInfo<Admin> selectAdminWithPage(Integer page, Integer count) {
        PageHelper.startPage(page, count);

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("admin_id");
        List<Admin> adminsList = adminService.list(queryWrapper);

        PageInfo<Admin> pageInfo=new PageInfo<>(adminsList);
        return pageInfo;
    }

    @Override
    public Admin login(String username, String password,Integer role) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",username)
                .eq("password",password)
                .eq("role",role);
        Admin admin = adminDao.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param adminId 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Integer adminId) {
        return this.adminDao.queryById(adminId);
    }

    /**
     * 分页查询
     *
     * @param admin 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Admin> queryByPage(Admin admin, PageRequest pageRequest) {
        long total = this.adminDao.count(admin);
        return new PageImpl<>(this.adminDao.queryAllByLimit(admin, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin insert(Admin admin) {
        this.adminDao.insert(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin update(Admin admin) {
        this.adminDao.update(admin);
        return this.queryById(admin.getAdminId());
    }

    /**
     * 通过主键删除数据
     *
     * @param adminId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer adminId) {
        return this.adminDao.deleteById(adminId) > 0;
    }
}
